package cz.fel.via.timemanagement;

import java.util.ArrayList;
import java.util.List;

import cz.cvut.fel.via.kosapirest.Fasade;
import cz.cvut.fel.via.kosapirest.domain.Course;
import cz.cvut.fel.via.kosapirest.domain.Parallel;
import cz.cvut.fel.via.kosapirest.domain.Student;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ImageView.ScaleType;

//zobrazi predmety studenta
public class CoursesActivity extends Activity {

	private final static int ONLY_ME_NOTES = 15;
	private final static int ONLY_ME_EXAMS = 25;
	private final static long textSize = 18;
	String username;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.courses);

		username = (String) TimeManagement.getKey("username");
		if (username == null) {
			username = TimeManagement.getUser();
		}
		Fasade fasade = TimeManagement.getFasade();
		Student student = fasade.find(username, Student.class);

		// TODO kdyz je blbe username ;)

		TextView name = (TextView) findViewById(R.id.studentName);
		name.setText(student.getFirstName()+" "+student.getLastName());
		name.setGravity(Gravity.CENTER_HORIZONTAL);

		List<Parallel> paralels = fasade.findStudentParallels(username);
		List<String> coursesNames = new ArrayList<String>();
		for (Parallel parallel : paralels) {
			String courseName = parallel.getCourse();
			if (!coursesNames.contains(courseName)) {
				coursesNames.add(courseName);
			}
		}

		List<Course> courses = new ArrayList<Course>();
		for (String courseName : coursesNames) {
			courses.add(fasade.find(courseName, Course.class));
		}
		// courses obsahuje vsechny predmety studenta.

		LinearLayout placeForCourses = (LinearLayout) findViewById(R.id.courses);
		for (Course course : courses) {
			placeForCourses.addView(courseDisplay(this, course));
		}

	}

	private LinearLayout courseDisplay(final Context cnt, final Course c) {
		LinearLayout layout = new LinearLayout(cnt);
		layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT));
		layout.setOrientation(LinearLayout.VERTICAL);

		TextView name = new TextView(cnt);
		name.setText(c.getName());
		name.setTextSize(textSize);
		name.setTextColor(Color.CYAN);
		TextView code = new TextView(cnt);
		code.setText(c.getCode());
		code.setTextSize(textSize);
		code.setTextColor(Color.CYAN);
		Button detail = new Button(cnt);
		detail.setText(R.string.detail);
		detail.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT));
		detail.setTextSize(textSize-4);
		detail.setWidth(80);
		detail.setHeight(40);
		detail.setOnClickListener(new OnClickListener () {
			public void onClick(View v) {
				TimeManagement.putKey("course", c.getCode());
				
				Intent intent = new Intent(cnt, OneCourseActivity.class);
				startActivity(intent);
			}
		});
		
		TextView separator = new TextView(cnt);
		separator.setText(" - ");
		separator.setTextSize(textSize);
		
		LinearLayout secondLine = new LinearLayout(cnt);
		secondLine.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT));
		secondLine.setOrientation(LinearLayout.HORIZONTAL);
		
		ImageView sep = new ImageView(cnt);
        sep.setImageResource(R.drawable.horizontal);
        sep.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT ));
        sep.setScaleType(ScaleType.FIT_XY);
        sep.setPadding(5, 5, 2, 2);

		secondLine.addView(code);
		secondLine.addView(separator);
		secondLine.addView(name);

		layout.addView(secondLine);
		layout.addView(detail);
		layout.addView(sep);

		return layout;
	}

	public void notes(View v) {
		if (username.equals(TimeManagement.getUser())) {
			Intent intent = new Intent(this, NoteActivity.class);
			startActivity(intent);
		} else {
			showDialog(ONLY_ME_NOTES);
		}
	}

	public void schedule(View v) {
		TimeManagement.putKey("username", username);

		Intent intent = new Intent(this, ScheduleActivity.class);
		startActivity(intent);
	}

	public void exams(View v) {
		if (username.equals(TimeManagement.getUser())) {
			Intent intent = new Intent(this, ExamActivity.class);
			startActivity(intent);
		} else {
			showDialog(ONLY_ME_EXAMS);
		}
	}

	public void student(View v) {
		TimeManagement.putKey("username", username);

		Intent intent = new Intent(this, StudentActivity.class);
		startActivity(intent);
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case ONLY_ME_NOTES:
			AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
			alertbox.setMessage(R.string.onlyMeNotes);
			alertbox.setNeutralButton("Ok", null);
			return alertbox.show();
		case ONLY_ME_EXAMS:
			AlertDialog.Builder alertbox1 = new AlertDialog.Builder(this);
			alertbox1.setMessage(R.string.onlyMeExams);
			alertbox1.setNeutralButton("Ok", null);
			return alertbox1.show();
		}
		return null;
	}
}
